package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    //    根据条件分页查询公寓列表
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    //----------------------------------------------------------------
    //根据ID获取公寓详细信息
    @Autowired
//            公寓的标签
            LabelInfoMapper labelInfoMapper;
    @Autowired
//            公寓的配套
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
//            公寓的图片
    GraphInfoMapper graphInfoMapper;
    @Autowired
//            公寓的费用
    FeeValueMapper feeValueMapper;
    //----------------------------------------------------------------
//    保存或更新公寓信息
//    公寓配套
    @Autowired
    ApartmentFacilityMapper apartmentFacilityMapper;
    //    公寓标签
    @Autowired
    ApartmentLabelMapper apartmentLabelMapper;
    //    公寓费用
    @Autowired
    ApartmentFeeValueMapper apartmentFeeValueMapper;

    //     公寓配套server
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    //    公寓标签server
    @Autowired
    ApartmentLabelService apartmentLabelService;
    //    公寓费用server
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    //    公寓图片server
    @Autowired
    GraphInfoService graphInfoService;

    //    省份
    @Autowired
    ProvinceInfoMapper provinceInfoMapper;
    //    市
    @Autowired
    CityInfoMapper cityInfoMapper;
    //    区
    @Autowired
    DistrictInfoMapper districtInfoMapper;
    //----------------------------------------------------------------
//根据id删除公寓信息
    @Autowired
    RoomInfoMapper roomInfoMapper;
    //----------------------------------------------------------------
    @Autowired
    RedisTemplate redisTemplate;

    //    根据条件分页查询公寓列表
    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(page, queryVo);
    }

    //    根据ID获取公寓详细信息
    @Override
    public ApartmentDetailVo getDetailById(Long id) {
//        封装一个返回对象
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
//        公寓基本信息
        ApartmentInfo apartmentInfo = this.getById(id);
//        将公寓基本信息拷贝到返回对象中
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        //sql查询公寓的标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelByApartId(id);
        //sql查询公寓的费用
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeByApartId(id);
        //sql查询公寓的配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityByApartId(id);
        //sql查询公寓的图片
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphByApartId(ItemType.APARTMENT, id);

//        set标签列表
        apartmentDetailVo.setLabelInfoList(labelInfoList);
//        set费用列表
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
//        set配套列表
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
//        set图片列表
        apartmentDetailVo.setGraphVoList(graphVoList);
        return apartmentDetailVo;
    }

    //    保存或更新公寓信息
    @Override
    public void saveOrUpdateApart(ApartmentSubmitVo apartmentSubmitVo) {
//        id不为空，更新公寓信息
        Boolean isUpdate = apartmentSubmitVo.getId() != null;
//        获取省市区id
        Long provinceId = apartmentSubmitVo.getProvinceId();
        Long cityId = apartmentSubmitVo.getCityId();
        Long districtId = apartmentSubmitVo.getDistrictId();
//        通过id查询省市区
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(provinceId);
        CityInfo cityInfo = cityInfoMapper.selectById(cityId);
        DistrictInfo districtInfo = districtInfoMapper.selectById(districtId);
//        设置省市区名称
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());
//        拿到 ApartmentSubmitVo继承的 ApartmentInfo 公寓信息
        this.saveOrUpdate(apartmentSubmitVo);
//         拿到公寓ID
        Long apartmentId = apartmentSubmitVo.getId();
//        id不为空 修改公寓信息
        if (isUpdate) {
//-----------------删除公寓关系表中的数据--------------------
//            删除配套
            apartmentFacilityMapper.delete(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId, apartmentId));
//            删除标签
            apartmentLabelMapper.delete(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, apartmentId));
//            删除费用
            apartmentFeeValueMapper.delete(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId, apartmentId));
//            删除图片
            graphInfoMapper.delete(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, apartmentId).eq(GraphInfo::getItemType, ItemType.APARTMENT));
        }
//        id为空 新增公寓信息
//-----------------新增公寓关系表中的数据--------------------
//        "公寓配套"
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
//        判断公寓配套的id是否为空
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            //封装关系表中的信息
            List<ApartmentFacility> list = new ArrayList<>();
//            遍历公寓配套的id
            for (Long facilityInfoId : facilityInfoIds) {
//                封装关系表中的信息
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentId).facilityId(facilityInfoId).build();
//                添加到集合中
                list.add(apartmentFacility);
            }
            //批量添加公寓和配套的id
            apartmentFacilityService.saveBatch(list);
        }


//       "公寓标签"
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
//        判断公寓标签的id是否为空
        if (!CollectionUtils.isEmpty(labelIds)) {
            //保存公寓和标签的id
            List<ApartmentLabel> apartmentLabels = new ArrayList<>();
//            遍历公寓标签的id
            for (Long labelId : labelIds) {
//                封装关系表中的信息
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentId).labelId(labelId).build();
//                添加到集合中
                apartmentLabels.add(apartmentLabel);
            }
//            批量保存公寓和标签的id
            apartmentLabelService.saveBatch(apartmentLabels);
        }

//       "公寓杂费值"
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
//        判断公寓杂费的id是否为空
        if (!CollectionUtils.isEmpty(feeValueIds)) {
//            保存公寓和杂费的id
            List<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
//            遍历公寓杂费的id
            for (Long feeValueId : feeValueIds) {
//                封装关系表中的信息
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(apartmentId).feeValueId(feeValueId).build();
//                添加到集合中
                apartmentFeeValues.add(apartmentFeeValue);
            }
//            批量保存公寓和杂费的id
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }
//        "公寓图片"
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
//        判断公寓图片的id是否为空
        if (!CollectionUtils.isEmpty(graphVoList)) {
//            保存公寓和图片的id
            List<GraphInfo> graphInfos = new ArrayList<>();

            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
//                图片的名字
                graphInfo.setName(graphVo.getName());
//                图片的url
                graphInfo.setUrl(graphVo.getUrl());
//                外键
                graphInfo.setItemId(apartmentId);
//                公寓类型
                graphInfo.setItemType(ItemType.APARTMENT);
//                添加到集合中
                graphInfos.add(graphInfo);

                String url = graphVo.getUrl();
                String fileName = url.substring(url.indexOf("lease/") + 6);
                String dbImageKey = "db_image:key";
                redisTemplate.opsForSet().add(dbImageKey, fileName);
            }
//            批量保存公寓和图片的id
            graphInfoService.saveBatch(graphInfos);
        }

    }

    //    根据id删除公寓信息
    @Override
    public void removeApartmentById(Long id) {
//        获取公寓下是否有房间 有房间给出提示不能删
        Long count = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
//        删除公寓信息
        this.removeById(id);
//        删除配套
        apartmentFacilityMapper.delete(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId, id));
//        删除标签
        apartmentLabelMapper.delete(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, id));
//        删除费用
        apartmentFeeValueMapper.delete(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId, id));
//        删除图片
        graphInfoMapper.delete(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.APARTMENT));
    }
}




